package com.bupt.smart.ui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bupt.smart.model.Goods;
import com.bupt.smart.smart.*;
import com.bupt.smart.ui.activity.MarketActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by HH on 2016/4/8.
 */
public class MarketAdapter extends BaseAdapter {
    private List<Goods> datas;
    private LayoutInflater layoutInflater;
    //private Context context;
    private MarketActivity context;
    public MarketAdapter(MarketActivity context,List<Goods> datas){
        this.context= context;
        layoutInflater = LayoutInflater.from(context);
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    //绘制ListView每一行
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ZuJian zujian ;


        if(convertView==null){
            zujian = new ZuJian();
            convertView = layoutInflater.inflate(R.layout.listview_market_r,null);
            zujian.name = (TextView) convertView.findViewById(R.id.shangpinname);
            zujian.price = (TextView) convertView.findViewById(R.id.shangpinprice);
            zujian.image = (ImageView) convertView.findViewById(R.id.shangpintupian);
            zujian.add = (ImageView) convertView.findViewById(R.id.imageadd);
            zujian.sub = (ImageView) convertView.findViewById(R.id.imagesub);
            zujian.num = (TextView) convertView.findViewById(R.id.goodsnum);
            zujian.id = (TextView) convertView.findViewById(R.id.shangpinID);
            convertView.setTag(zujian);
        }
        else
            zujian = (ZuJian) convertView.getTag();
        //绑定数据，添加事件触发
        zujian.name.setText(datas.get(position).getShopname());
        zujian.price.setText(datas.get(position).getShopprice()+"");
        //zujian.image.setImageResource(datas.get(position).image);
        zujian.image.setImageBitmap(datas.get(position).getBitmapGood());
        zujian.add.setImageResource(R.mipmap.btn_icon_market_add);
        zujian.sub.setImageResource(R.mipmap.btn_icon_market_sub);
        zujian.num.setText("0");
        zujian.id.setText(datas.get(position).getId()+"");
        zujian.num.setTextColor(Color.rgb(192, 192, 192));//初始个数为0，颜色灰色
        zujian.sub.setVisibility(View.INVISIBLE);//初始隐藏减号
        //添加商品到购物车
        zujian.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String perprice = zujian.price.getText().toString();
                zujian.num.setTextColor(Color.rgb(0, 0, 0));
                zujian.sub.setVisibility(View.VISIBLE);
                int num = Integer.parseInt(zujian.num.getText().toString());
                num++;
                zujian.num.setText(num + "");
                context.shopCart(perprice, 1);
                context.setCart(1, zujian.id.getText().toString(), zujian.name.getText().toString(), zujian.price.getText().toString());
            }
        });
        //删除购物车商品
        zujian.sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String perprice = zujian.price.getText().toString();
                int num = Integer.parseInt(zujian.num.getText().toString());
                context.setCart(2,zujian.id.getText().toString(),zujian.name.getText().toString(),zujian.price.getText().toString());
                if(num<=0) {
                    zujian.sub.setVisibility(View.INVISIBLE);
                    zujian.num.setTextColor(Color.rgb(192, 192, 192));//商品数量减少到0变灰
                }
                else
                {
                    num--;
                    if(num==0)
                    {
                        zujian.sub.setVisibility(View.INVISIBLE);
                        zujian.num.setTextColor(Color.rgb(192, 192, 192));
                    }

                    zujian.num.setText(num+"");
                    context.shopCart(perprice,2);
                }
            }
        });
        return convertView;
    }
    public class ZuJian{
        public TextView name;
        public TextView price;
        public ImageView image;
        public ImageView add;
        public ImageView sub;
        public TextView num;
        public TextView id;
    }
}
